package org.robot.scm.platform.server.handler;

import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import java.io.IOException;
import java.util.concurrent.CompletableFuture;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * robot服务重发
 */
@Slf4j
@NoArgsConstructor
public class DeployRobotHandler extends BaseHandler implements HttpHandler {

    @Override
    public String getUri() {
        return "/deployRobot";
    }

    @Override
    public void handle(HttpExchange exchange) throws IOException {

        // 当启动一个新的进程的时候当前系统直接退出
        CompletableFuture.runAsync(() -> {
            ProcessBuilder processBuilder = new ProcessBuilder("cmd", "/c", "start", "/min", "start.bat");
            try {
                processBuilder.start();
                System.exit(0);
            } catch (IOException e) {
                log.error("error", e);
            }
        });

        sendSuccessResponse(exchange, "机器人发布成功");
    }
}
